﻿
using Engine;
using Engine.Graphics;
using Game;

namespace Mekiasm
{
    public class FishingRod : ItemFlat
    {
        public static int Index;
        public bool IsFishing = false;
        public ThrowPickable ThrowPickable;
        public Pickable Pickable;


        public FishingRod() : base( "钓鱼竿", MekiasmInit.GetFlatTexture(MekiasmInit.R2, "FishingRod"))
        {
            
        }

        public override bool OnUse(Ray3 aim, ComponentMiner componentMiner, TerrainRaycastResult? raycast)
        {
            SaveItemSystem.GetBlockData(componentMiner.ActiveBlockValue, out Common.SaveItem saveItem);
            if (IsFishing)
            {
                if (SubsystemMekBlockBehavior.SubsystemThrowSystem.Pickables.Contains(ThrowPickable) && !ThrowPickable.FlyToPosition.HasValue)
                {
                    SaveItemSystem.ChangePower(componentMiner.ActiveBlockValue, -1);
                    Vector3 vector = SubsystemMekBlockBehavior.SubsystemThrowSystem.GetStartPosition(componentMiner.ComponentCreature.ComponentBody);
                    if (ILibrary.random.Bool(0.9f)) saveItem.AddSlotItems(0, ThrowPickable.ActiveValue, 1);
                    ThrowPickable.FlyToPosition = vector;
                }
            }
            else
            {

                if (saveItem == null || saveItem.GetSlotCount(0) == 0)
                {
                    DisplayMsg(componentMiner.ComponentPlayer,"请先上鱼饵", Color.Yellow);
                }
                else
                {
                    SubsystemMekBlockBehavior.SubsystemThrowSystem.PickableRemoved = (pick) =>
                    {
                        IsFishing = false;
                    };
                    ThrowPickable = SubsystemMekBlockBehavior.SubsystemThrowSystem.AddPickable(componentMiner.ComponentCreature.ComponentBody, saveItem.GetSlotValue(0), FishingRodG.Index, aim.Direction * 10, null);
                    saveItem.RemoveSlotItems(0, 1);
                    IsFishing = true;
                }
            }
            return true;
        }

        public override bool IsCrushWhenNoPower()
        {
            return true;
        }

        public override int GetBasePower()
        {
            return 0;
        }
        public override int GetMaxPower()
        {
            return 64;
        }

        public override bool OnEditBlock(int x, int y, int z, int value, ComponentPlayer componentPlayer)
        {
            SaveItemSystem.GetBlockData(value, out Common.SaveItem saveItem);
            if (saveItem == null)
            {
                int newValue = SaveItemSystem.StoreBlockData(value, 32, 0, 1);
                componentPlayer.ComponentMiner.Inventory.RemoveSlotItems(componentPlayer.ComponentMiner.Inventory.ActiveSlotIndex, 1);
                componentPlayer.ComponentMiner.Inventory.AddSlotItems(componentPlayer.ComponentMiner.Inventory.ActiveSlotIndex, newValue, 1);
                SaveItemSystem.GetBlockData(newValue, out Common.SaveItem saveItem2);
                saveItem = saveItem2;
            }
            componentPlayer.ComponentGui.ModalPanelWidget = new FishingRodWidget(saveItem, componentPlayer.ComponentMiner);
            return true;
        }

        public override bool OnEditInventoryItem(int value, IInventory inventory, int slotIndex, ComponentPlayer componentPlayer)
        {
            SaveItemSystem.GetBlockData(value, out Common.SaveItem saveItem);
            if (saveItem == null)
            {
                int newValue = SaveItemSystem.StoreBlockData(value, GetMaxPower(), 0,1);
                componentPlayer.ComponentMiner.Inventory.RemoveSlotItems(componentPlayer.ComponentMiner.Inventory.ActiveSlotIndex, 1);
                componentPlayer.ComponentMiner.Inventory.AddSlotItems(componentPlayer.ComponentMiner.Inventory.ActiveSlotIndex, newValue, 1);
                SaveItemSystem.GetBlockData(newValue, out Common.SaveItem saveItem2);
                saveItem = saveItem2;
            }
            componentPlayer.ComponentGui.ModalPanelWidget = new FishingRodWidget(saveItem, componentPlayer.ComponentMiner);
            return true;
        }

    }
}
